Featured Post

Python Variables and Data Types Explained with Examples for Beginners

Python Variables and Data Types Explained with Examples for Beginners


Introduction

Python is one of the easiest programming languages for beginners because its syntax is simple and easy to read. In this post, we will learn about variables and data types, which are the building blocks of Python programming.

What is a Variable?

A variable is a name used to store data in a program. Data can be number, text, or object. You can think of it as a container that holds a value and whenever we need that data we need to call that container by its name.

Example

        Python Code
            name = "Haarsh"
            age = 20
In this example, name stores text, and age stores a number.

Rules for Declaring Variables

  • Must start with a "letter" or "UnderScrore _"
  • Cannot start with a number
  • Cannot use Python keywords
  • Cannot use White Spaces in Variable Names.
  • Uses of Variable Declaration in Python

    1. Store Data :Variables keep values in memory.
                  Python Code
                      age = 21
      You can use age later in the program whenever you need.
    2. Reuse Values : Instead of writing the same value repeatedly:
    3.             Python Code
                      pi = 3.14
                      area = pi * r * r
    4. Perform Calculations :Variables help in mathematical operations.
                  Python Code
                      a = 5
                      b = 3
                      sum = a + b
                      print(sum)
    5. Store User Input
    6.             Python Code
                      name = input("Enter your name: ")
                      print(name)
    7. Update Values Dynamically :Variables can change during execution.
    8.             Python Code
                      score = 10
                      score = score + 5
      Now score becomes 15.

    Common Mistakes in Python Variables

    1. Using a Variable Before Declaring It
              Python Code
                  print(name)
                  name = "Rahul" 
              Python Code
                  name = "Rahul"
                  print(name)

    2. Forgetting Quotes for Strings
              Python Code
                  name = Rahul
      Python thinks Rahul is another variable.
              Python Code
                  name = "Rahul"

    3. Using Python Keywords as Variable Names
              Python Code
                  class = 10
      Explainnation:- class is a Python keyword.
              Python Code
                  class_name = 10

    4. Starting Variable Name with a Number
              Python Code
                  1name = "Ali" 
              Python Code
                  name1 = "Ali"

    5. Case Sensitivity ConfusionPython treats uppercase and lowercase differently.
              Python Code
                  age = 20
                  print(Age)
              Python Code
                  age = 20
                  print(age)

    6. Using Spaces in Variable Names
              Python Code
                  student name = "Amit"❌
              Python Code
                  student_name = "Amit"✅

    7. Using Special Characters
      ❌ We cannot use special characters like &,*,-,=,@ etc.
              Python Code
                  user-name = "Sam"
      ✅ We only use underscrore
              Python Code
                  user_name = "Sam"

    8. Choosing Poor Variable Names :

      This Type of variable is hard to understand by reader. Suppose you are a developer and you take variable name "X" for name. When code tester read the code they are unable to know what value does variable "X" hold untill they excute the code side by side. Instead of "X" if developer take variable is "name" than tester easily understand that the use of that variable
              Python Code
                  a = 50000     ❌
                      Reader will confused What is variable "a" 
                  
              Python Code
                  salary = 50000    ✅
                      Reader will easily understand about variable salary
                  

    9. Overwriting Important Variables

              Python Code
                  x = 10 
                  x = "hello" 
      Now x is no longer a number.

    10. Confusing in EqualTo "=" and Double EqualTo "=="

      = → assignment
      = = → comparison
          Python Code
                  if x = 5:
                  if x == 5:

    What are Data Types?

    Data types tell Python what kind of value is stored in a variable. It represents what kind of operation can be done on a particular data. Different data types are used for different kinds of data.

    Common Python Data Types

    Data Type

    Example

    Description

    int 5 For Whole numbers
    float 5.5 For Decimal numbers
    str "Python" For Text
    bool True For Boolean values

    Here is the some example

            Python Code
                
                x = 10
                y = 3.5
                message = "Hello, Python"
                is_student = True
            
    Here: x is an integer.
    y is a float.
    message is a string.
    is_student is a boolean.

    Checking the Data Type

    You can check the data type of a variable using the `type()` function.
        Python Code
            num = 5
            print(type(num))
        Python Code Output
            class 'int'

    Taking Input from the User

    Python allows you to take input from the user using the `input()` function. But remember, input is always taken as text by default.
    Here is the Example:
            Python Code
            
            name = input("Enter your name: ")
            print("Hello", name)
            
            Python Code Output
            Enter your name: Haarsh
            Hello Haarsh

    Type Conversion

    If you want to perform calculations, you must convert text input into a number using `int()` or `float()`. Example
            Python Code
            
                num1 = int(input("Enter first number: "))
                num2 = int(input("Enter second number: "))
                print("Sum:", num1 + num2)
            
        
    This program takes two numbers from the user and adds them.
            Python Code Output
            
                Enter first number: 5
                Enter second number: 3
                Sum: 8
            
            

    Full Example

            Python Code
            
                name = input("Enter your name: ")
                age = int(input("Enter your age: "))
    
                print("Hello", name)
                print("Next year, you will be", age + 1)
            
        
            Python Code Output
                Enter your name: Ravi
                Enter your age: 20
                Hello Ravi
                Next year, you will be 21
    Explanation
  • input() takes the name and age from the user
  • age + 1 increases the age by 1
  • Then it prints both results
  • This example shows how variables, data types, input, and type conversion work together.

    Key Takeaways

  • Variables are used to store data values in Python.
  • Python does not require declaring variable types explicitly.
  • Common Python data types include int, float, str, bool, list, tuple, and dict.
  • The type() function is used to check the data type of a variable.
  • Python is a dynamically typed programming language.
  • Variable names should be meaningful and follow Python naming rules.
  • Type conversion allows changing one data type into another using functions like int(), float(), and str().
  • Proper knowledge of data types helps avoid programming errors and improves code readability.
  • Understanding variables and data types is essential for writing Python programs effectively.
  • Lists are mutable, while tuples are immutable in Python.